# -*- coding:utf-8 -*-
"""
@File       :   handle_log.py
@Author     :   LINING
@Time       :   2023/10/31 11:26
@Description :   打印日志
"""
import logging
import os
import time
from Handle.handle_yaml import LOG_PATH


class Log:
    def __init__(self):
        self.file_name = os.path.join(LOG_PATH, str(time.strftime('%Y%m%d'))+str(time.time()).split('.')[0]+'.txt')
        # date_fmt = '%Y%m%d %H%M%S'
        # 输出日志格式
        fmt_log = '%(asctime)s- %(levelname)s-%(message)s '
        self.formatter = logging.Formatter(fmt=fmt_log)
        self.logger = logging.getLogger()

    def log_info(self, info):
        self.logger.setLevel(logging.INFO)
        # 输出到控制台
        sh = logging.StreamHandler()
        sh.setLevel(logging.INFO)
        # 写文件
        fh = logging.FileHandler(filename=self.file_name, mode='a', encoding='utf-8')
        fh.setLevel(logging.INFO)
        # 给handler添加formatter
        fh.setFormatter(self.formatter)
        sh.setFormatter(self.formatter)
        # 给logger添加handler
        self.logger.addHandler(fh)
        self.logger.addHandler(sh)
        # 打印日志
        self.logger.info(info)
        # 移除handler
        self.logger.removeHandler(fh)
        self.logger.removeHandler(sh)

    def log_error(self, info):
        self.logger.setLevel(logging.ERROR)
        # 输出到控制台
        sh = logging.StreamHandler()
        sh.setLevel(logging.ERROR)
        # 写文件
        fh = logging.FileHandler(filename=self.file_name, mode='a', encoding='utf-8')
        fh.setLevel(logging.ERROR)
        # 给handler添加formatter
        fh.setFormatter(self.formatter)
        sh.setFormatter(self.formatter)
        # 给logger添加handler
        self.logger.addHandler(fh)
        self.logger.addHandler(sh)
        # 打印日志
        self.logger.error(info)
        # 移除handler
        self.logger.removeHandler(fh)
        self.logger.removeHandler(sh)


log = Log()
# log.log_error('11')

